package com.songhq.filterandintercept.intercepter;


import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StaticdataInterceptor implements HandlerInterceptor {
	 
	//org.slf4j给日志系统提供统一的接口，自己不实现
    final static Logger logerRoot=LoggerFactory.getLogger(StaticdataInterceptor.class);
	final static Logger logerFile=LoggerFactory.getLogger("LOG_CALL_INFO");
	
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    	  //当拦截器运行时产生异常且没有捕捉时，会被调用它的方法进行捕获
    	//在返回不在/staticData的路径下的404还是正常的返回
        String url = httpServletRequest.getAttribute("orignUrl").toString();
        int httpCode = httpServletResponse.getStatus();
        if(httpCode != HttpServletResponse.SC_OK || StringUtils.isNotEmpty(url)){
            try {
                ringContent(httpServletResponse,url);
                
            } catch (Exception e) {
            }
        }
        //return false时拦截一下所有请求，直接走返回链
      return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
    public void ringContent(HttpServletResponse httpServletResponse,String url){
        try {
        	  httpServletResponse.reset();
            httpServletResponse.setCharacterEncoding("UTF-8");
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
            httpServletResponse.getWriter().write(url);
        } catch (Exception e) {
        	
        	
        }
    }
}
